//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SpreadCdsHelper.h"
using namespace Cephei::QL::Termstructures::Credit;
#include <gen/QL/Times/Period.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Termstructures/DefaultProbabilityTermStructure.h>
#include <gen/QL/Termstructures/Credit/CdsHelper.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Credit::CSpreadCdsHelper::CSpreadCdsHelper (Double runningSpread, Cephei::QL::Times::IPeriod^ tenor, Int32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, QL::Times::FrequencyEnum frequency, QL::Times::BusinessDayConventionEnum paymentConvention, QL::Times::DateGeneration::RuleEnum rule, Cephei::QL::Times::IDayCounter^ dayCounter, Double recoveryRate, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve, Microsoft::FSharp::Core::FSharpOption<Boolean>^ settlesAccrual, Microsoft::FSharp::Core::FSharpOption<Boolean>^ paysAtDefaultTime) : CCdsHelper(CSpreadCdsHelper::typeid)
{
    CPeriod^ _Ctenor;
    CCalendar^ _Ccalendar;
    CDayCounter^ _CdayCounter;
    CYieldTermStructure^ _CdiscountCurve;
    try
    {
#ifdef HANDLE
        _phSpreadCdsHelper = NULL;
#endif
        QuantLib::Rate _runningSpread = (QuantLib::Rate)ValueHelper::Convert (runningSpread); //d
        _Ctenor = safe_cast<CPeriod^> (tenor);
        _Ctenor->Lock();
        QuantLib::Period& _tenor = static_cast<QuantLib::Period&> (_Ctenor->GetReference ()); 
        QuantLib::Integer _settlementDays = (QuantLib::Integer)ValueHelper::Convert (settlementDays); //d
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        QuantLib::Frequency _frequency = (QuantLib::Frequency)frequency ;
        QuantLib::BusinessDayConvention _paymentConvention = (QuantLib::BusinessDayConvention)paymentConvention ;
        QuantLib::DateGeneration::Rule _rule = (QuantLib::DateGeneration::Rule)rule ;
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        QuantLib::Real _recoveryRate = (QuantLib::Real)ValueHelper::Convert (recoveryRate); //d
        _CdiscountCurve = safe_cast<CYieldTermStructure^> (discountCurve);
        _CdiscountCurve->Lock();
        Handle<QuantLib::YieldTermStructure>& _discountCurve = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdiscountCurve->GetHandle ()); 
        bool _settlesAccrual = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (settlesAccrual) ? (bool)ValueHelper::Convert (settlesAccrual->Value) : true); //4
        bool _paysAtDefaultTime = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (paysAtDefaultTime) ? (bool)ValueHelper::Convert (paysAtDefaultTime->Value) : true); //4
        _ppSpreadCdsHelper = new boost::shared_ptr<QuantLib::SpreadCdsHelper> (new QuantLib::SpreadCdsHelper ( _runningSpread,  _tenor,  _settlementDays,  _calendar,  _frequency,  _paymentConvention,  _rule,  _dayCounter,  _recoveryRate,  _discountCurve,  _settlesAccrual,  _paysAtDefaultTime ));
        SetCdsHelper (boost::dynamic_pointer_cast<QuantLib::CdsHelper> (*_ppSpreadCdsHelper));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ctenor != nullptr) _Ctenor->Unlock();
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
        if (_CdiscountCurve != nullptr) _CdiscountCurve->Unlock();
    }
}
Cephei::QL::Termstructures::Credit::CSpreadCdsHelper::CSpreadCdsHelper (Cephei::QL::IQuote^ runningSpread, Cephei::QL::Times::IPeriod^ tenor, Int32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, QL::Times::FrequencyEnum frequency, QL::Times::BusinessDayConventionEnum paymentConvention, QL::Times::DateGeneration::RuleEnum rule, Cephei::QL::Times::IDayCounter^ dayCounter, Double recoveryRate, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve, Microsoft::FSharp::Core::FSharpOption<Boolean>^ settlesAccrual, Microsoft::FSharp::Core::FSharpOption<Boolean>^ paysAtDefaultTime) : CCdsHelper(CSpreadCdsHelper::typeid)
{
    CQuote^ _CrunningSpread;
    CPeriod^ _Ctenor;
    CCalendar^ _Ccalendar;
    CDayCounter^ _CdayCounter;
    CYieldTermStructure^ _CdiscountCurve;
    try
    {
#ifdef HANDLE
        _phSpreadCdsHelper = NULL;
#endif
        _CrunningSpread = safe_cast<CQuote^> (runningSpread);
        _CrunningSpread->Lock();
        Handle<QuantLib::Quote>& _runningSpread = static_cast<Handle<QuantLib::Quote>&> (_CrunningSpread->GetHandle ()); 
        _Ctenor = safe_cast<CPeriod^> (tenor);
        _Ctenor->Lock();
        QuantLib::Period& _tenor = static_cast<QuantLib::Period&> (_Ctenor->GetReference ()); 
        QuantLib::Integer _settlementDays = (QuantLib::Integer)ValueHelper::Convert (settlementDays); //d
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        QuantLib::Frequency _frequency = (QuantLib::Frequency)frequency ;
        QuantLib::BusinessDayConvention _paymentConvention = (QuantLib::BusinessDayConvention)paymentConvention ;
        QuantLib::DateGeneration::Rule _rule = (QuantLib::DateGeneration::Rule)rule ;
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        QuantLib::Real _recoveryRate = (QuantLib::Real)ValueHelper::Convert (recoveryRate); //d
        _CdiscountCurve = safe_cast<CYieldTermStructure^> (discountCurve);
        _CdiscountCurve->Lock();
        Handle<QuantLib::YieldTermStructure>& _discountCurve = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdiscountCurve->GetHandle ()); 
        bool _settlesAccrual = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (settlesAccrual) ? (bool)ValueHelper::Convert (settlesAccrual->Value) : true); //4
        bool _paysAtDefaultTime = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (paysAtDefaultTime) ? (bool)ValueHelper::Convert (paysAtDefaultTime->Value) : true); //4
        _ppSpreadCdsHelper = new boost::shared_ptr<QuantLib::SpreadCdsHelper> (new QuantLib::SpreadCdsHelper ( _runningSpread,  _tenor,  _settlementDays,  _calendar,  _frequency,  _paymentConvention,  _rule,  _dayCounter,  _recoveryRate,  _discountCurve,  _settlesAccrual,  _paysAtDefaultTime ));
        SetCdsHelper (boost::dynamic_pointer_cast<QuantLib::CdsHelper> (*_ppSpreadCdsHelper));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CrunningSpread != nullptr) _CrunningSpread->Unlock();
        if (_Ctenor != nullptr) _Ctenor->Unlock();
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
        if (_CdiscountCurve != nullptr) _CdiscountCurve->Unlock();
    }
}
Cephei::QL::Termstructures::Credit::CSpreadCdsHelper::CSpreadCdsHelper (boost::shared_ptr<QuantLib::SpreadCdsHelper>& childNative, Object^ owner) : CCdsHelper(CSpreadCdsHelper::typeid)
{
#ifdef HANDLE
	_phSpreadCdsHelper = NULL;
#endif
	_ppSpreadCdsHelper = &childNative;
    _ppCdsHelper = new boost::shared_ptr<QuantLib::CdsHelper> (boost::dynamic_pointer_cast<QuantLib::CdsHelper> (*_ppSpreadCdsHelper));
}
Cephei::QL::Termstructures::Credit::CSpreadCdsHelper::CSpreadCdsHelper (QuantLib::SpreadCdsHelper& childNative, Object^ owner) : CCdsHelper(CSpreadCdsHelper::typeid)
{
#ifdef HANDLE
	_phSpreadCdsHelper = NULL;
#endif
	_ppSpreadCdsHelper = new boost::shared_ptr<QuantLib::SpreadCdsHelper> (&childNative);
    _ppCdsHelper = new boost::shared_ptr<QuantLib::CdsHelper> (boost::dynamic_pointer_cast<QuantLib::CdsHelper> (*_ppSpreadCdsHelper));
    _SpreadCdsHelperOwner = owner;
    _CdsHelperOwner = owner;
}

Cephei::QL::Termstructures::Credit::CSpreadCdsHelper::CSpreadCdsHelper (CSpreadCdsHelper^ copy) : CCdsHelper(CSpreadCdsHelper::typeid)
{
#ifdef HANDLE
	_phSpreadCdsHelper = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSpreadCdsHelper = new boost::shared_ptr<QuantLib::SpreadCdsHelper> (copy->GetShared());
        _ppCdsHelper = new boost::shared_ptr<QuantLib::CdsHelper> (boost::dynamic_pointer_cast<QuantLib::CdsHelper> (*_ppSpreadCdsHelper));
    }
}
Cephei::QL::Termstructures::Credit::CSpreadCdsHelper::CSpreadCdsHelper (PLATFORM::Type^ t) : CCdsHelper(CSpreadCdsHelper::typeid)
{
#ifdef HANDLE
	_phSpreadCdsHelper = NULL;
#endif
	if (!t->IsSubclassOf(CSpreadCdsHelper::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Credit::CSpreadCdsHelper::CSpreadCdsHelper (QuantLib::Handle<QuantLib::SpreadCdsHelper>& childNative, Object^ owner)  : CCdsHelper(CSpreadCdsHelper::typeid)
{
	_phSpreadCdsHelper = &childNative;
	_ppSpreadCdsHelper = &static_cast<boost::shared_ptr<QuantLib::SpreadCdsHelper>>(childNative.currentLink());
    _ppCdsHelper = new boost::shared_ptr<QuantLib::CdsHelper> (boost::dynamic_pointer_cast<QuantLib::CdsHelper> (*_ppSpreadCdsHelper));
    _SpreadCdsHelperOwner = owner;
}
Cephei::QL::Termstructures::Credit::CSpreadCdsHelper::CSpreadCdsHelper (QuantLib::Handle<QuantLib::SpreadCdsHelper> childNative)  : CCdsHelper(CSpreadCdsHelper::typeid)
{
	_phSpreadCdsHelper = &childNative;
	_ppSpreadCdsHelper = &static_cast<boost::shared_ptr<QuantLib::SpreadCdsHelper>>(childNative.currentLink());
    _ppCdsHelper = new boost::shared_ptr<QuantLib::CdsHelper> (boost::dynamic_pointer_cast<QuantLib::CdsHelper> (*_ppSpreadCdsHelper));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Credit::CSpreadCdsHelper::CSpreadCdsHelper (QuantLib::SpreadCdsHelper childNative)  : CCdsHelper(CSpreadCdsHelper::typeid)
{
#ifdef HANDLE
	_phSpreadCdsHelper = NULL;
#endif
	_ppSpreadCdsHelper = new boost::shared_ptr<QuantLib::SpreadCdsHelper> (new QuantLib::SpreadCdsHelper (childNative));
    _ppCdsHelper = new boost::shared_ptr<QuantLib::CdsHelper> (boost::dynamic_pointer_cast<QuantLib::CdsHelper> (*_ppSpreadCdsHelper));
}
#endif

Cephei::QL::Termstructures::Credit::CSpreadCdsHelper::~CSpreadCdsHelper ()
{
    if (_ppSpreadCdsHelper != NULL)
    {
	    delete _ppSpreadCdsHelper;
        _ppSpreadCdsHelper = NULL;
    }
}
Cephei::QL::Termstructures::Credit::CSpreadCdsHelper::!CSpreadCdsHelper ()
{
    if (_ppSpreadCdsHelper != NULL)
    {
	    delete _ppSpreadCdsHelper;
    }
}
QuantLib::SpreadCdsHelper& Cephei::QL::Termstructures::Credit::CSpreadCdsHelper::GetReference ()
{
    if (_ppSpreadCdsHelper == NULL) throw REFNEW NativeNullException ();
	return **_ppSpreadCdsHelper;
}
boost::shared_ptr<QuantLib::SpreadCdsHelper>& Cephei::QL::Termstructures::Credit::CSpreadCdsHelper::GetShared ()
{
    if (_ppSpreadCdsHelper == NULL) throw REFNEW NativeNullException ();
	return *_ppSpreadCdsHelper;
}
QuantLib::SpreadCdsHelper* Cephei::QL::Termstructures::Credit::CSpreadCdsHelper::GetPointer ()
{
    if (_ppSpreadCdsHelper == NULL) throw REFNEW NativeNullException ();
	return &**_ppSpreadCdsHelper;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SpreadCdsHelper>& Cephei::QL::Termstructures::Credit::CSpreadCdsHelper::GetHandle ()
{
	if (_phSpreadCdsHelper == NULL)
	{
		_phSpreadCdsHelper = new Handle<QuantLib::SpreadCdsHelper> (*_ppSpreadCdsHelper);
	}
	return *_phSpreadCdsHelper;
}
#endif
bool Cephei::QL::Termstructures::Credit::CSpreadCdsHelper::HasNative () 
{
	return (_ppSpreadCdsHelper != NULL);
}

Double Cephei::QL::Termstructures::Credit::CSpreadCdsHelper::ImpliedQuote::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSpreadCdsHelper)->impliedQuote ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Credit::ISpreadCdsHelper^ Cephei::QL::Termstructures::Credit::CSpreadCdsHelper_Factory::Create (Double runningSpread, Cephei::QL::Times::IPeriod^ tenor, Int32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, QL::Times::FrequencyEnum frequency, QL::Times::BusinessDayConventionEnum paymentConvention, QL::Times::DateGeneration::RuleEnum rule, Cephei::QL::Times::IDayCounter^ dayCounter, Double recoveryRate, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve, Microsoft::FSharp::Core::FSharpOption<Boolean>^ settlesAccrual, Microsoft::FSharp::Core::FSharpOption<Boolean>^ paysAtDefaultTime)
{
    return REFNEW CSpreadCdsHelper ( runningSpread,  tenor,  settlementDays,  calendar,  frequency,  paymentConvention,  rule,  dayCounter,  recoveryRate,  discountCurve,  settlesAccrual,  paysAtDefaultTime);
}
Cephei::QL::Termstructures::Credit::ISpreadCdsHelper^ Cephei::QL::Termstructures::Credit::CSpreadCdsHelper_Factory::Create (Cephei::QL::IQuote^ runningSpread, Cephei::QL::Times::IPeriod^ tenor, Int32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, QL::Times::FrequencyEnum frequency, QL::Times::BusinessDayConventionEnum paymentConvention, QL::Times::DateGeneration::RuleEnum rule, Cephei::QL::Times::IDayCounter^ dayCounter, Double recoveryRate, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve, Microsoft::FSharp::Core::FSharpOption<Boolean>^ settlesAccrual, Microsoft::FSharp::Core::FSharpOption<Boolean>^ paysAtDefaultTime)
{
    return REFNEW CSpreadCdsHelper ( runningSpread,  tenor,  settlementDays,  calendar,  frequency,  paymentConvention,  rule,  dayCounter,  recoveryRate,  discountCurve,  settlesAccrual,  paysAtDefaultTime);
}
